<?php defined('SYSPATH') or die('No direct script access.');
/**
 * 扩展 Kohana_Validation 类
 *
 * @author  Yellow.Chow <aultoale@gmail.com>
 * @version $Id: validation.php 143 2012-04-10 10:23:55Z yellow.chow $
 * @license http://www.gnu.org/licenses/gpl-3.0.html    GPL 3
 */
class Validation extends Kohana_Validation
{

    /**
     * 获取指定的数据
     *
     * @param  string  $offset
     * @param  mixed   $default
     * @return mixed
     */
    public function get($offset, $default = NULL)
    {
        return isset($this->_data[$offset]) ? $this->_data[$offset] : $default;
    }

    /**
     * 允许使用魔术方法访问数据
     *
     * @param   string  $offset
     * @return  mixed
     */
    public function __get($offset)
    {
        return $this->get($offset);
    }

    /**
     * 判断数据是否被设定
     *
     * @param  string  $offset
     * @return bool
     */
    public function has($offset)
    {
        return isset($this->_data[$offset]);
    }

    /**
     * 允许使用魔术方法判断数据
     *
     * @param  string  $offset
     * @return bool
     */
    public function __isset($offset)
    {
        return $this->has($offset);
    }

    /**
     * 重写 errors 方法，使用 I18n 作为消息输出
     *
     * @param  string  $package
     * @param  mixed   $translate
     * @return array
     */
    public function errors($package = NULL, $translate = TRUE)
    {
        if ($package === NULL)
        {
            // Return the error list
            return $this->_errors;
        }

        // 加载 Validation 默认语言
        I18n::import('validation');

        // 加载指定的语言包
        I18n::import($package);

        $messages = array();

        foreach ($this->_errors as $field => $set)
        {
            list($error, $params) = $set;

            // Start the translation values list
            $values = array(
                ':field' => $this->_translate($package, $field),
                ':value' => Arr::get($this, $field),
            );

            if (is_array($values[':value']))
            {
                // All values must be strings
                $values[':value'] = implode(', ', Arr::flatten($values[':value']));
            }

            if ($params)
            {
                foreach ($params as $key => $value)
                {
                    if (is_array($value))
                    {
                        // All values must be strings
                        $value = implode(', ', Arr::flatten($value));
                    }
                    elseif (is_object($value))
                    {
                        // Objects cannot be used in message files
                        continue;
                    }

                    // Check if a label for this parameter exists
                    if (isset($this->_labels[$value]))
                    {
                        // Use the label as the value, eg: related field name for "matches"
                        $value = $this->_translate($package, $this->_labels[$value]);
                    }

                    // Add each parameter as a numbered value, starting from 1
                    $values[':param'.($key + 1)] = $value;
                }
            }

            // Set the message for this field
            $messages[$field] = $this->_translate($package, $field, $error, $values);
        }

        return $messages;
    }

    /**
     * 使用 I18n 转换错误消息
     *
     * @param  string  $package
     * @param  string  $field
     * @param  string  $error
     * @param  array   $values
     * @return string
     */
    protected function _translate($package, $field, $error = NULL, array $values = NULL)
    {
        if ($error === NULL)
        {
            $message = $this->__("{$package}.{$field}", $values);

            if ($message === NULL)
            {
                // 尝试指定字段名
                $message = $this->__("{$package}.{$field}.field", $values);
            }

            if ($message === NULL)
            {
                // 格式化输出
                $message = ":{$field}";
            }
        }
        else
        {
            // 尝试转换
            $message = $this->__("{$package}.{$field}.{$error}", $values);

            if ($message === NULL)
            {
                // 使用默认消息
                $message = $this->__("validation.{$error}", $values);
            }

            if ($message === NULL)
            {
                // 格式化输出
                $message = "{$package}.{$field}.{$error}";
            }
        }

        return $message;
    }

    /**
     * 尝试翻译转换，失败则返回 NULL
     *
     * @param  string  $string
     * @param  array   $values
     * @return string|NULL
     */
    protected function __($string, array $values = NULL)
    {
        $message = __($string, $values);

        return ($message === $string) ? NULL : $message;
    }


}